package com.controller;

import com.bean.User;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    //注入一个UserService。
    @Resource
    private UserService userService;

    @RequestMapping("/login")
    //参数名和login.jsp中的一致，则不用加@RequestParam
    public String login(String userName, String userPassword, HttpServletRequest request) {
        User user = userService.checkLogin(userName, userPassword);
        if (user == null) {
            //对应login.jsp的${tips}。
            request.setAttribute("tips", "用户名或者密码错误！");
            //账号密码不匹配就到登录界面。
            return "/login.jsp";
        } else {
            //把用户信息放到Session；
            request.getSession().setAttribute("user", user);
            return "redirect:/index.jsp";
        }
    }
}
